package com.lc.guessTheWords.sprite;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Typeface;
import android.util.Log;

import com.lc.guessTheWords.R;
import com.lc.guessTheWords.core.GameSystem;
import com.lc.guessTheWords.core.StageManager;
import com.lc.guessTheWords.layer.GameLayer;
import com.wiyun.engine.nodes.Button;
import com.wiyun.engine.nodes.Label;
import com.wiyun.engine.nodes.Node;
import com.wiyun.engine.types.WYColor3B;

/**
 * 虚拟键盘
 * 
 * @author Administrator
 * 
 */
public class VisualKeyBoard extends Node {

	static final int columns = 8;
	static final int rows = 3;
	static List<String> keys;

	Button btnLamp;
	Label lampCount;

	public VisualKeyBoard() {
		keys = new ArrayList<String>();
		keys.add("A");
		keys.add("B");
		keys.add("C");
		keys.add("D");
		keys.add("E");
		keys.add("F");
		keys.add("G");
		keys.add("H");
		keys.add("J");
		keys.add("K");
		keys.add("L");
		keys.add("M");
		keys.add("N");
		keys.add("O");
		keys.add("P");
		keys.add("Q");
		keys.add("R");
		keys.add("S");
		keys.add("T");
		keys.add("W");
		keys.add("X");
		keys.add("Y");
		keys.add("Z");

		initUI();

		// Button button = Button.make(R.drawable.word_button_normal,
		// R.drawable.word_button_pressed, R.drawable.word_button_normal,
		// R.drawable.word_button_normal, new TargetSelector(this,
		// "onButtonClicked(int)", new Object[] { 1 }));
	}

	private void initUI() {
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				int index = i * columns + j;
				float x = GameSystem.KeyWidth * j + GameSystem.KeyWidth / 2.0f;
				float y = (GameSystem.KeyHeight * 3) - GameSystem.KeyHeight * i
						- GameSystem.KeyHeight / 2.0f;

				String keyStr = "";
				if (index < keys.size()) {
					keyStr = keys.get(index);
					KeyItemBlock key = new KeyItemBlock(keyStr);
					key.setPosition(x, y);
					this.addChild(key);
				} else {
					// 提示按钮
					btnLamp = Button.make(R.drawable.lamp_button_normal,
							R.drawable.lamp_button_pressed, this,
							"onLampClicked");
					btnLamp.setPosition(x, y);
					this.addChild(btnLamp);

					// 可用提示次数
					lampCount = Label.make(
							"" + StageManager.instance.getLampCount(), 28,
							Typeface.BOLD);
					lampCount.setAlpha(222);
					lampCount.setColor(WYColor3B.make(30, 30, 30));
					lampCount.setPosition(x, y);
					this.addChild(lampCount);
				}

			}
		}

	}

	/**
	 * 更新提示数量
	 */
	public void updateLampCount() {
		lampCount.setText("" + StageManager.instance.getLampCount());
	}

	public void onLampClicked() {
		Log.d("click", "lamp");
		GameLayer.instance.touchKey("lamp");
	}
}
